#include "CentralTab.h"
#include "Central/CentralTabWidget.h"
#include "MainWindow.h"
#include "Managers/DataManager.h"

#include "CentralTab_p.h"

Q_UNIQUE_ID_DECLARE(CentralTab)

CentralTab::CentralTab(QWidget *parent) : CentralTab(*new CentralTabPrivate(), parent) {
}

CentralTab::~CentralTab() {
    destructId();
}

CentralTab::Type CentralTab::type() const {
    return NoTab;
}

QByteArray CentralTab::mimeText() const {
    return tabText().toUtf8();
}

QString CentralTab::tabText() const {
    return QString();
}

bool CentralTab::carryMime() const {
    Q_DC(CentralTab);
    return d->carryMime;
}

void CentralTab::setCarryMime(bool carryMime) {
    Q_D(CentralTab);
    d->carryMime = carryMime;
}

QString CentralTab::filename() const {
    Q_DC(CentralTab);
    return d->filename;
}

void CentralTab::setFilename(const QString &filename) {
    Q_D(CentralTab);
    d->filename = filename;
    filenameSet(filename);
}

bool CentralTab::isEdited() const {
    Q_DC(CentralTab);
    return d->edited;
}

CentralTabWidget *CentralTab::tabWidget() const {
    QWidget *p = parentWidget();
    if (!p) {
        return nullptr;
    }
    return qobject_cast<CentralTabWidget *>(p->parentWidget());
}

MainWindow *CentralTab::mainWindow() const {
    return qWindow;
}

CentralTab::CentralTab(CentralTabPrivate &d, QWidget *parent) : BaseTab(parent), d_ptr(&d) {
    d_ptr->q_ptr = this;
    d.init();
    initId();
}

void CentralTab::setTabName(const QString &tabName) {
    Q_D(CentralTab);
    BaseTab::setTabName(d->setTabNameProxy(tabName));
}

void CentralTab::updateTabName() {
    Q_D(CentralTab);
    setTabName(d->updateTabNameProxy());
}

void CentralTab::setEdited(bool edited) {
    Q_D(CentralTab);
    d->edited = edited;
}

void CentralTab::filenameSet(const QString &filename) {
    Q_UNUSED(filename)
    updateTabName();
}

bool CentralTab::load() {
    return false;
}

bool CentralTab::save() {
    return false;
}

bool CentralTab::saveAs() {
    return false;
}

bool CentralTab::restore() {
    return false;
}

void CentralTab::undo() {
}

void CentralTab::redo() {
}

void CentralTab::selectAll() {
}

void CentralTab::deselect() {
}

void CentralTab::awake() {
}

void CentralTab::sleep() {
}

void CentralTab::enter() {
}

void CentralTab::leave() {
}

void CentralTab::change() {
}

bool CentralTab::acceptClose() {
    return true;
}

void CentralTab::close() {
    QTabWidget *tabs = tabWidget();
    if (tabs) {
        emit tabs->tabCloseRequested(tabs->indexOf(this));
    }
}

QList<QMenu *> CentralTab::menuList() const {
    Q_DC(CentralTab);
    return d->menuListProxy();
}

QString CentralTab::TypeToString(CentralTab::Type type) {
    QString res;
    switch (type) {
    case Welcome: {
        res = "welcome";
        break;
    }
    case Tuning: {
        res = "tuning";
        break;
    }
    case Voice: {
        res = "voice";
        break;
    }
    case Keyboard: {
        res = "keyboard";
        break;
    }
    case Setting: {
        res = "setting";
        break;
    }
    default: {
        res = "unknown";
        break;
    }
    }
    return res;
}
